package com.losdun.aftersales.controller;

import com.losdun.aftersales.domain.Install;
import com.losdun.aftersales.manager.NoticeManager;
import com.losdun.aftersales.service.IInstallService;
import com.losdun.common.annotation.Log;
import com.losdun.common.core.controller.BaseController;
import com.losdun.common.core.domain.AjaxResult;
import com.losdun.common.core.page.TableDataInfo;
import com.losdun.common.enums.BusinessType;
import com.losdun.common.utils.poi.ExcelUtil;
import com.losdun.system.service.ISysUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 售后安装Controller
 * 
 * @author swordye
 * @date 2025-08-19
 */
@RestController
@RequestMapping("/aftersales/install")
@Slf4j
public class InstallController extends BaseController
{
    @Autowired
    private IInstallService installService;
    @Autowired
    private ISysUserService sysUserService;
    @Autowired
    private NoticeManager noticeManager;

    /**
     * 查询售后安装列表
     */
    @PreAuthorize("@ss.hasPermi('aftersales:install:list')")
    @GetMapping("/list")
    public TableDataInfo list(Install install)
    {
        startPage();
        List<Install> list = installService.selectInstallList(install);
        list.forEach(item ->{
            if(item.getWorkerId() != null){
                item.setWorker(sysUserService.selectUserById(item.getWorkerId()));
            }
        });
        return getDataTable(list);
    }

    /**
     * 导出售后安装列表
     */
    @PreAuthorize("@ss.hasPermi('aftersales:install:export')")
    @Log(title = "售后安装", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Install install)
    {
        List<Install> list = installService.selectInstallList(install);
        ExcelUtil<Install> util = new ExcelUtil<Install>(Install.class);
        util.exportExcel(response, list, "售后安装数据");
    }

    /**
     * 获取售后安装详细信息
     */
    @PreAuthorize("@ss.hasPermi('aftersales:install:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(installService.selectInstallById(id));
    }

    /**
     * 新增售后安装
     */
    @PreAuthorize("@ss.hasPermi('aftersales:install:add')")
    @Log(title = "售后安装", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Install install)
    {
        return toAjax(installService.insertInstall(install));
    }

    @PreAuthorize("@ss.hasPermi('aftersales:install:add')")
    @PostMapping( "/agentAdd")
    public AjaxResult agentAdd(@RequestBody Install install) {
        try {
            installService.appInstallSave(install);
            return success();
        } catch (Exception e) {
            log.error(e.getMessage(),e);
            return error(e.getMessage());
        }
    }

    /**
     * 修改售后安装
     */
    @PreAuthorize("@ss.hasPermi('aftersales:install:edit')")
    @Log(title = "售后安装", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Install install)
    {
        boolean isChangeWorker=false;
        try {
            isChangeWorker=installService.checkChangeWorker(install);
            int ret=installService.updateInstall(install);
            if(isChangeWorker){
                noticeManager.installAssignWorkerSendNotice(install);
            }
            return toAjax(ret);
        } catch (Exception e) {
            log.error("修改安装工单失败",e.getMessage(),e);
            return error(e.getMessage());
        }

    }

    /**
     * 删除售后安装
     */
    @PreAuthorize("@ss.hasPermi('aftersales:install:remove')")
    @Log(title = "售后安装", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(installService.deleteInstallByIds(ids));
    }
}
